package com.syz.mall.controller;

import com.syz.mall.pojo.*;
import com.syz.mall.pojo.vo.CartVO;
import com.syz.mall.pojo.vo.OrderVO;
import com.syz.mall.service.ICartService;
import com.syz.mall.service.IOrderService;
import com.syz.mall.service.IShippingService;
import com.syz.mall.service.IUserService;
import com.syz.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IShippingService shippingService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/getOrderConfirmPage")
    public String getOrderConfirmPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<UserAddress> addressList = userService.selectAllAddressByUserId(user.getId());
        List<CartVO> cartVOList = cartService.selectByUserIdAndChecked(user.getId());
        BigDecimal totalPrice=new BigDecimal(0);
        for(CartVO cartVO :cartVOList){
            totalPrice= totalPrice.add(cartVO.getProductPrice().multiply(BigDecimal.valueOf(cartVO.getQuantity())));
        }
        model.addAttribute("addressList", addressList);
        model.addAttribute("cartVOList", cartVOList);
        model.addAttribute("total", totalPrice);
        return "order_confirm";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Order order, HttpSession session) {
        System.out.println("OrderController.add");
        User user = (User) session.getAttribute("user");
        order.setUserId(user.getId());
        boolean isSuccess = orderService.add(order);
        return isSuccess ? Result.ok("创建订单成功") : Result.error("创建订单失败，可能商品库存不足");
    }

    @RequestMapping("/getOrderRefundPage")
    public String getORP(){
        return "/orderCenter/order_refund";
    }
    @RequestMapping("/getUserCollectionPage")
    public String getUCP(HttpSession session,Model model){
        User user= (User) session.getAttribute("user");
        List<Product> list=userService.selectUserCollect(user.getId());
        model.addAttribute("list",list);
        return "/orderCenter/user_collection";
    }

    @RequestMapping("/getUserOrderPage")
    public String getOrderListPage(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<OrderVO> list = orderService.selectByUserId(user.getId());
        model.addAttribute("list", list);
        return "/orderCenter/user_order";
    }
    @RequestMapping("/updateOrder")
    @ResponseBody
    public Result updateOrder(Order order){
        Boolean isSuccess=orderService.updateOrder(order);
        return isSuccess ? Result.ok("更改成功") : Result.error("更改失败");
    }
    @RequestMapping("/delUnReal")
    @ResponseBody
    public  Result delUnReal(Long id){
        Boolean isSucess=orderService.delUnReal(id);
        return  isSucess? Result.ok("删除成功"):Result.error("删除失败");
    }

}
